package com.yk.faceapplication;


import android.Manifest;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.yk.faceapplication.entity.BankInfo;
import com.yk.faceapplication.entity.Member;
import com.yk.faceapplication.entity.Purpose;
import com.yk.faceapplication.entity.StudyInfo;
import com.yk.faceapplication.util.LogDebugger;

import java.util.List;

public class MyApplication extends Application {

    public static String Cookie;

    private long showWrongMsgTime;// 异常信息显示时间,用于屏蔽间隔较小的提示信息

    private List<BankInfo> banks;
    private List<StudyInfo> studyInfos;
    private List<StudyInfo> bankTypes;
    private List<StudyInfo> loanTerms;
    private List<Purpose> works;

    private List<Purpose> purposes;

    private List<StudyInfo> contacts;

    public List<StudyInfo> getLoanTerms() {
        return loanTerms;
    }

    public void setLoanTerms(List<StudyInfo> loanTerms) {
        this.loanTerms = loanTerms;
    }

    public List<StudyInfo> getContacts() {
        return contacts;
    }

    public void setContacts(List<StudyInfo> contacts) {
        this.contacts = contacts;
    }

    public List<Purpose> getPurposes() {
        return purposes;
    }

    public void setPurposes(List<Purpose> purposes) {
        this.purposes = purposes;
    }

    public List<Purpose> getWorks() {
        return works;
    }

    public void setWorks(List<Purpose> works) {
        this.works = works;
    }

    public List<StudyInfo> getBankTypes() {
        return bankTypes;
    }

    public void setBankTypes(List<StudyInfo> bankTypes) {
        this.bankTypes = bankTypes;
    }

    public List<StudyInfo> getStudyInfos() {
        return studyInfos;
    }

    public void setStudyInfos(List<StudyInfo> studyInfos) {
        this.studyInfos = studyInfos;
    }

    public List<BankInfo> getBanks() {
        return banks;
    }

    public void setBanks(List<BankInfo> banks) {
        this.banks = banks;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    private boolean homePageGetMember = false;

    public boolean isHomePageGetMember() {
        return homePageGetMember;
    }

    public void setHomePageGetMember(boolean homePageGetMember) {
        this.homePageGetMember = homePageGetMember;
    }

    private String token;

    private String memberId;

    public String getMemberId() {
        return memberId;
    }

    public void setMemberId(String memberId) {
        this.memberId = memberId;
    }

    private Member member;

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    /**
     * 请求队列
     */
    private RequestQueue rQueue;

    public RequestQueue getrQueue() {
        return rQueue;
    }

    public void setrQueue(RequestQueue rQueue) {
        this.rQueue = rQueue;
    }

    /**
     * 密钥
     */
    private final String secretKey = "J9UIZ1N5";

    private LocationManager locationManager;
    private String locationProvider;

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    private Location location;


    public String getSecretKey() {
        return secretKey;
    }

    public long getShowWrongMsgTime() {
        return showWrongMsgTime;
    }

    public void setShowWrongMsgTime(long showWrongMsgTime) {
        this.showWrongMsgTime = showWrongMsgTime;
    }




    @Override
    public void onCreate() {
        super.onCreate();
        LogDebugger.info("init application");
        rQueue = Volley.newRequestQueue(this);

        // 创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).writeDebugLogs() // 打印log信息
                .build();

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(configuration);

        //获取地理位置管理器
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //获取所有可用的位置提供器
        List<String> providers = locationManager.getProviders(true);
        if (providers.contains(LocationManager.GPS_PROVIDER)) {
            //如果是GPS
            locationProvider = LocationManager.GPS_PROVIDER;
        } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            //如果是Network
            locationProvider = LocationManager.NETWORK_PROVIDER;
        } else {
//            Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
            return;
        }
        //获取Location
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = locationManager.getLastKnownLocation("locationProvider");

        locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);
        LogDebugger.info((location == null) + "!");


    }

    /**
     * LocationListern监听器
     * 参数：地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器
     */

    LocationListener locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle arg2) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onLocationChanged(Location location) {
            //如果位置发生变化,重新显示
            LogDebugger.info("location:" + location.getLatitude() + "  " + location.getLongitude());
            if (location != null) {
                setLocation(location);
            }

        }
    };


    @Override
    public void onTerminate() {

        super.onTerminate();

    }

}
